package com.example.androidmychapter.Service;


import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.androidmychapter.util.RecordingUtil;

import java.io.File;

public class MyService extends Service {
    private static final int REQUEST_MANAGE_EXTERNAL_STORAGE_CODE = 1;
    private static final int REQUEST_CODE = 2;
    private static int i = 0;
    private RecordingUtil recordingUtil;


    private static final String TAG = "MyService";

    @Override
    public void onCreate() {
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {

            private String outputFile;

            @Override
            public void run() {
                performLongRunningTask();
            }

            private void performLongRunningTask() {

                String directory = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + "YourDirectory.3gp").getPath();
                Log.d("directory", directory);
//                File externalFilesDir = getExternalFilesDir(null);
//                outputFile = new File(getExternalFilesDir(null), "/" + (i++) +  "recording.3gp").getPath();
//                outputFile = new File(externalFilesDir, (i++) + "recording.3gp").getPath();
                recordingUtil = new RecordingUtil();
                recordingUtil.startRecording(directory);
            }
        }).start();
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        recordingUtil.stopRecording();
    }
}
